<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/6
 * Time: 14:09
 */

namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\service\WxNotify;
use app\api\validate\IDMustBePostiveInt;
use app\api\service\Pay as payService;
class Pay extends BaseController
{
    protected $beforeActionList = [
        'checkExclusiveScope'=>['only'=>'getPreOrder'],
    ];

    /*
     * @预订单及支付:微信文档地址https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_1&index=1
     * $id:用户订单号id
     * */
    public function getPreOrder($id)
    {
        (new IDMustBePostiveInt())->goCheck();
        $pay = new payService();
        return $pay->pay();
    }

    public function redirectNotify()
    {
        //通知频率12/30/180/1800/1800/1800/1800/3600 单位:秒

        //1.检测库存量,超卖
        //2.更新此订单status状态
        //3.减库存
        //如果成功处理,返回微信成功处理的消息,否则,需要返回没有成功处理.

        //特点:请求方式:post,参数:xml格式;不会携带参数

        $notify = new WxNotify();
        $notify->Handle();
    }

    //接收参数,并转发到处理接口
    public function receiveNotify()
    {
        //通知频率12/30/180/1800/1800/1800/1800/3600 单位:秒

        //1.检测库存量,超卖
        //2.更新此订单status状态
        //3.减库存
        //如果成功处理,返回微信成功处理的消息,否则,需要返回没有成功处理.

        //特点:请求方式:post,参数:xml格式;不会携带参数
//        $notify = new WxNotify();
//        $notify->Handle();

        $xmlData = file_get_contents('php://input');
        $result = curl_post_raw('http://www.wx.com/api/v1/pay/re_notify?'.$xmlData);
        return $result;
    }
}